package cn.iocoder.yudao.module.game.controller.admin.addMoney;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.game.controller.admin.addMoney.vo.*;
import cn.iocoder.yudao.module.game.dal.dataobject.addMoney.AddMoneyDO;
import cn.iocoder.yudao.module.game.service.addMoney.AddMoneyService;

@Tag(name = "管理后台 - 加钱")
@RestController
@RequestMapping("/game/add-money")
@Validated
public class AddMoneyController {

    @Resource
    private AddMoneyService addMoneyService;

    @PostMapping("/create")
    @Operation(summary = "创建加钱")
    @PreAuthorize("@ss.hasPermission('game:add-money:create')")
    public CommonResult<Long> createAddMoney(@Valid @RequestBody AddMoneySaveReqVO createReqVO) {
        return success(addMoneyService.createAddMoney(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新加钱")
    @PreAuthorize("@ss.hasPermission('game:add-money:update')")
    public CommonResult<Boolean> updateAddMoney(@Valid @RequestBody AddMoneySaveReqVO updateReqVO) {
        addMoneyService.updateAddMoney(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除加钱")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('game:add-money:delete')")
    public CommonResult<Boolean> deleteAddMoney(@RequestParam("id") Long id) {
        addMoneyService.deleteAddMoney(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得加钱")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('game:add-money:query')")
    public CommonResult<AddMoneyRespVO> getAddMoney(@RequestParam("id") Long id) {
        AddMoneyDO addMoney = addMoneyService.getAddMoney(id);
        return success(BeanUtils.toBean(addMoney, AddMoneyRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得加钱分页")
    @PreAuthorize("@ss.hasPermission('game:add-money:query')")
    public CommonResult<PageResult<AddMoneyRespVO>> getAddMoneyPage(@Valid AddMoneyPageReqVO pageReqVO) {
        PageResult<AddMoneyDO> pageResult = addMoneyService.getAddMoneyPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AddMoneyRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出加钱 Excel")
    @PreAuthorize("@ss.hasPermission('game:add-money:export')")
    @OperateLog(type = EXPORT)
    public void exportAddMoneyExcel(@Valid AddMoneyPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<AddMoneyDO> list = addMoneyService.getAddMoneyPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "加钱.xls", "数据", AddMoneyRespVO.class,
                        BeanUtils.toBean(list, AddMoneyRespVO.class));
    }

}